package com.java_senior_8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class StudentManager {
    //尖括号类是参数化类型（即studentList里面只能装Student类型）￥￥￥￥
    private static List<Student> studentList=null;
    static{
        studentList=new ArrayList<Student>(){
            {
                add(new Student(1000,"Tom","男",20));
                add(new Student(1001,"张三","男",18));
                add(new Student(1002,"刘明笑","男",19));
                add(new Student(1003,"刘凯","男",23));
                add(new Student(1004,"张三丰","男",24));

            }
        };
    }
    //新增学员
    /*public static boolean addStudent(int id,String name,String sex,int age){

    }*/

    //检查学号是否唯一
    private static boolean exists(int id){
        for (int i = 0,size = studentList.size();i < size; i++) {
            if(id == studentList.get(i).getId()){
                return true;
            }
        }
        return false;
    }
    //添加新学员
    public static boolean addStudent(Student student){
        //学号唯一
        if(!exists(student.getId())){ //如果学号不重复，可以添加
            studentList.add(student);
            return true;
        }
        else{
            return false; //如果学号重复，不能添加
        }
    }

    private static int getIndex(int id){
        int index = -1;
        for (int i = 0,size = studentList.size(); i < size; i++) {
            if(id == studentList.get(i).getId()){
                index = i;
                break;
            }
        }
        return index;
    }

    //根据学号修改学员信息
    public static boolean updateStudent(Student student){
        if(exists(student.getId())){
            int index = getIndex(student.getId());
            if(index != -1) {
                studentList.set(index, student);
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    //根据学号删除指定学员
    public static boolean deleteStudentById(int id){
        int index = getIndex(id);
        if(index != -1){
            studentList.remove(index);
            return true;
        }
        else{
            return false;
        }
    }
    //根据姓名模糊查询学员信息列表
    public static List<Student> getStudentsByName(String name){
        List<Student> tempList = new ArrayList<>();
        for (int i = 0 , size = studentList.size(); i < size; i++) {
            Student student = studentList.get(i);
            if(student.getName().contains(name)){
                tempList.add(student);
            }
        }
        return tempList;
    }

public static void showAllStudent(){
    for (int i = 0,size = studentList.size();i < size; i++) {
        System.out.println(studentList.get(i).getId()+studentList.get(i).getSex()+studentList.get(i).getName());
    }
}

    public static  ArrayList<Student> getStudentList(int id) {
        Iterator iterator=studentList.iterator();
        ArrayList<Student> temp=new ArrayList<Student>();
        while(iterator.hasNext()){
           Student student= (Student) iterator.next();
            if (student.getId()==id){
               temp.add(student);

            }
        }
        return temp;
    }
/* public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("你好");
        //报错：list.add(1);
    }*/
}
